package com.scalar.chiptrack.users.notification.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.users.dao.UserDAO;
import com.scalar.chiptrack.users.notification.forms.NotificationForm;
import com.scalar.chiptrack.users.notification.dao.NotificationDAO;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * User: KP
 * Date: Sep 3, 2006
 * Time: 5:08:54 PM
*/

public class NotificationsSaveCommand  extends ActionCommand
{
    private static final String ACTION_NAME = "/userNotificatonSetup.do";


    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) throws Exception
    {
        ActionForward actionForward         = mapping.findForward ( "loadUserNotification" );
        NotificationForm notificationForm   = ( NotificationForm ) form;

        HashMap errors                      = new HashMap();
        HashMap info                        = new HashMap();

        DBTransaction dbTransaction = null;
        boolean success = false;

        request.setAttribute( SessionKeys.INFO_MESSAGES, info );
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );

        HttpSession mSession              = request.getSession();
        User user                         = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }
        else
        {
            if ( user.isActionAllowed ( Functionality.NOTIFICATION_SETUP , User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute ( "showMessage", "FALSE" );
            }
            else
            {
                request.setAttribute ( "showMessage", "TRUE" );
            }
        }

        try
        {
            dbTransaction = DBTransaction.getInstance();
            dbTransaction.begin();

            /* SAVING NOTIFICATION DETAILS WHICH ARE ASSOCIATED WITH PARTICULAR USER-ID*/
            NotificationDAO.deleteUserNotificationDetails( dbTransaction, notificationForm );
            NotificationDAO.savedUserNotificationDetails( dbTransaction, notificationForm );

            /* SAVING VENDOR ACCESS DETAILS WHICH ARE ASSOCIATED WITH PARTICULAR USER-ID*/
            NotificationDAO.deleteUserVendorAccessDetails ( dbTransaction, notificationForm );
            NotificationDAO.savedUserVendorAccessDetails( dbTransaction, notificationForm );

            success = true;

        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        finally
        {
            if( dbTransaction != null )
            {
                if( success == true )
                {
                    try
                    {
                        dbTransaction.commit();
                        info.put ( "User Notification and Vendors Access Information Saved Successfully ", null );
                    }
                    catch( Exception e )
                    {
                        e.printStackTrace();
                        errors.put ( "Error while committing the database ", null );
                    }
               }
                else
                {
                    try
                    {
                        dbTransaction.rollback();
                        errors.put ( "User Notification and Vendors Access Information saving aborted", null );
                    }
                    catch( Exception e )
                    {
                        e.printStackTrace();
                        errors.put ( "Error while rolling back the User Notification and Vendors Access Information", null );
                    }
                }
            }
        }

        try
        {
            notificationForm.setUserId( null );
            notificationForm.setEmailId( null );
            notificationForm.setVendors( null );
            notificationForm.setSelectedNotifications( null );

            ArrayList userList = UserDAO.loadAllUserIDs( false );

            request.setAttribute( "userList", userList );
            request.setAttribute( "action", ACTION_LOAD );
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }

        return actionForward;
    }
}
